define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'sport/area/index' + location.search,
                    add_url: 'sport/area/add',
                    edit_url: 'sport/area/edit',
                    del_url: 'sport/area/del',
                    multi_url: 'sport/area/multi',
                    import_url: 'sport/area/import',
                    table: 'sport_area',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'area_name', title: __('Area_name'), operate: 'LIKE'},
                        {field: 'status', title: __('Status'), searchList: {"normal":__('Status normal'),"hidden":__('Status hidden')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {   
                          field: 'operate', 
                          title: __('Operate'), 
                          table: table, 
                          events: Table.api.events.operate, 
                          buttons: [
                              {   
                                  name: 'createMiddle',
                                  text: __('生成中考模版'),
                                  icon: 'fa',
                                  classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                  icon: 'fa fa-magic',
                                  url: function (row) {
                                      return Fast.api.fixurl('sport/area/createTemplate?type=middle&area_id=' + row.id)
                                  },
                                  confirm: '确认要重新生成中考导入模版吗？',
                                  success: function (data, ret) {
                                      Layer.alert(ret.msg);
                                      //如果需要阻止成功提示，则必须使用return false;
                                      //return false;
                                  },
                                  error: function (data, ret) {
                                      console.log(data, ret);
                                      Layer.alert(ret.msg);
                                      return false;
                                  }
                              },  
                              {   
                                  name: 'createMiddle',
                                  text: __('生成高考模版'),
                                  icon: 'fa',
                                  classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                  icon: 'fa fa-magic',
                                  url: function (row) {
                                      return Fast.api.fixurl('sport/area/createTemplate?type=senior&area_id=' + row.id)
                                  },
                                  confirm: '确认要重新生成高考导入模版吗？',
                                  success: function (data, ret) {
                                      Layer.alert(ret.msg);
                                      //如果需要阻止成功提示，则必须使用return false;
                                      //return false;
                                  },
                                  error: function (data, ret) {
                                      console.log(data, ret);
                                      Layer.alert(ret.msg);
                                      return false;
                                  }
                              },  
                          ],  

                          formatter: Table.api.formatter.operate

                        }
                    ]
                ],
                showToggle: false,
                showColumns: false,
                showExport: false,
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
